using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace Watchdog2007 {

    public class WeeklySchedule : DailySchedule {

        public WeeklySchedule ( int id ) : base ( id ) { }

        public WeeklySchedule ( DataRow drow ) : base ( drow ) { }

        #region Day Properties
        public new ScheduleDaysOfWeek Days {
            get { return (ScheduleDaysOfWeek)base.Days; }
            set { base.Days = value.GetHashCode(); }
        }
        public bool Monday {
            get {
                if ((Days&ScheduleDaysOfWeek.Monday) == ScheduleDaysOfWeek.Monday)
                    return true;
                else
                    return false;
            }
            set {
                if (value) {
                    if ((Days & ScheduleDaysOfWeek.Monday) != ScheduleDaysOfWeek.Monday)
                        Days = Days | ScheduleDaysOfWeek.Monday;
                } else {
                    if ((Days & ScheduleDaysOfWeek.Monday) == ScheduleDaysOfWeek.Monday)
                        Days &= ~ScheduleDaysOfWeek.Monday;
                }
            }
        }
        public bool Tuesday {
            get {
                if ((Days & ScheduleDaysOfWeek.Tuesday) == ScheduleDaysOfWeek.Tuesday)
                    return true;
                else
                    return false;
            }
            set {
                if (value) {
                    if ((Days & ScheduleDaysOfWeek.Tuesday) != ScheduleDaysOfWeek.Tuesday)
                        Days = Days | ScheduleDaysOfWeek.Tuesday;
                } else {
                    if ((Days & ScheduleDaysOfWeek.Tuesday) == ScheduleDaysOfWeek.Tuesday)
                        Days &= ~ScheduleDaysOfWeek.Tuesday;
                }
            }
        }
        public bool Wednesday {
            get {
                if ((Days & ScheduleDaysOfWeek.Wednesday) == ScheduleDaysOfWeek.Wednesday)
                    return true;
                else
                    return false;
            }
            set {
                if (value) {
                    if ((Days & ScheduleDaysOfWeek.Wednesday) != ScheduleDaysOfWeek.Wednesday)
                        Days = Days | ScheduleDaysOfWeek.Wednesday;
                } else {
                    if ((Days & ScheduleDaysOfWeek.Wednesday) == ScheduleDaysOfWeek.Wednesday)
                        Days &= ~ScheduleDaysOfWeek.Wednesday;
                }
            }
        }
        public bool Thursday {
            get {
                if ((Days & ScheduleDaysOfWeek.Thursday) == ScheduleDaysOfWeek.Thursday)
                    return true;
                else
                    return false;
            }
            set {
                if (value) {
                    if ((Days & ScheduleDaysOfWeek.Thursday) != ScheduleDaysOfWeek.Thursday)
                        Days = Days | ScheduleDaysOfWeek.Thursday;
                } else {
                    if ((Days & ScheduleDaysOfWeek.Thursday) == ScheduleDaysOfWeek.Thursday)
                        Days &= ~ScheduleDaysOfWeek.Thursday;
                }
            }
        }
        public bool Friday {
            get {
                if ((Days&ScheduleDaysOfWeek.Friday) == ScheduleDaysOfWeek.Friday)
                    return true;
                else
                    return false;
            }
            set {
                if (value) {
                    if ((Days & ScheduleDaysOfWeek.Friday) != ScheduleDaysOfWeek.Friday)
                        Days = Days | ScheduleDaysOfWeek.Friday;
                } else {
                    if ((Days & ScheduleDaysOfWeek.Friday) == ScheduleDaysOfWeek.Friday)
                        Days &= ~ScheduleDaysOfWeek.Friday;
                }
            }
        }
        public bool Saturday {
            get {
                if ((Days&ScheduleDaysOfWeek.Saturday) == ScheduleDaysOfWeek.Saturday)
                    return true;
                else
                    return false;
            }
            set {
                if (value) {
                    if ((Days & ScheduleDaysOfWeek.Saturday) != ScheduleDaysOfWeek.Saturday)
                        Days = Days | ScheduleDaysOfWeek.Saturday;
                } else {
                    if ((Days & ScheduleDaysOfWeek.Saturday) == ScheduleDaysOfWeek.Saturday)
                        Days &= ~ScheduleDaysOfWeek.Saturday;
                }
            }
        }
        public bool Sunday {
            get {
                if ((Days&ScheduleDaysOfWeek.Sunday) == ScheduleDaysOfWeek.Sunday)
                    return true;
                else
                    return false;
            }
            set {
                if (value) {
                    if ((Days & ScheduleDaysOfWeek.Sunday) != ScheduleDaysOfWeek.Sunday)
                        Days = Days | ScheduleDaysOfWeek.Sunday;
                } else {
                    if ((Days & ScheduleDaysOfWeek.Sunday) == ScheduleDaysOfWeek.Sunday)
                        Days &= ~ScheduleDaysOfWeek.Sunday;
                }
            }
        }
        #endregion 

        public List<DateTime> GetExecutionTimes ( DateTime from, DateTime until ) {
            List<DateTime> Dates = new List<DateTime> ( );
            DateTime date = GetNextExecuteTime ( from );
            //Dates.Add ( date );
            while (date <= until.Add(HourStop)) {
                Dates.Add ( date );
                date = GetNextExecuteTime ( date );
            }
            return Dates;
        }

        public new DateTime GetNextExecuteTime (DateTime date) {
            DateTime Result = base.GetNextExecuteTime ( date );
           
            DayOfWeek day = Result.DayOfWeek;
            switch (day) {
                case DayOfWeek.Monday:
                    if (!Monday) {
                        Result = Result.Date.AddDays ( 1 );
                        Result = GetNextExecuteTime ( Result );
                    }
                    break;
                case DayOfWeek.Tuesday:
                    if (!Tuesday) {
                        Result = Result.Date.AddDays ( 1 );
                        Result = GetNextExecuteTime ( Result );
                    }
                    break;
                case DayOfWeek.Wednesday:
                    if (!Wednesday) {
                        Result = Result.Date.AddDays ( 1 );
                        Result = GetNextExecuteTime ( Result );
                    }
                    break;
                case DayOfWeek.Thursday:
                    if (!Thursday) {
                        Result = Result.Date.AddDays ( 1 );
                        Result = GetNextExecuteTime ( Result );
                    }
                    break;
                case DayOfWeek.Friday:
                    if (!Friday) {
                        Result = Result.Date.AddDays ( 1 );
                        Result = GetNextExecuteTime ( Result );
                    }
                    break;
                case DayOfWeek.Saturday:
                    if (!Saturday) {
                        Result = Result.Date.AddDays ( 1 );
                        Result = GetNextExecuteTime ( Result );
                    }
                    break;
                case DayOfWeek.Sunday:
                    if (!Sunday) {
                        Result = Result.Date.AddDays ( 1 );
                        Result = GetNextExecuteTime ( Result );
                    }
                    break;
            }
            //Result = GetNextExecuteTime ( Result );
            return Result;
        }

        public new DateTime GetNextExecuteTime() {
            return GetNextExecuteTime ( DateTime.Now );
        }

        public new DateTime SetNextExecuteTime () {
            NextRun = GetNextExecuteTime ( DateTime.Now );
            Update ( );
            return NextRun;
        }
    }
}
